//
//  LoginViewController.m
//  iTweet
//
//  Created by Dang Duc Nam (namk42c@yahoo.com) on 5/12/09.
//  Copyright 2009 Unitech. All rights reserved.
//


#import "LoginViewController.h"
#import "iTweetAppDelegate.h"

@implementation LoginViewController
@synthesize txtPassword;
@synthesize txtUsername;
@synthesize lblTitle;
@synthesize lblPassword;
@synthesize lblUsername;


/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
	appDelegate = (iTweetAppDelegate *)[[UIApplication sharedApplication] delegate];
	/*[btnLogin setTitle:NSLocalizedString(@"LoginKey",@"")];*/
	lblTitle.text = NSLocalizedString(@"LoginTitleKey",@"");
	lblUsername.text = NSLocalizedString(@"LoginUsernameKey",@"");
	lblPassword.text = NSLocalizedString(@"LoginPasswordKey",@"");
	
	//Initialize the toolbar
	toolbar = [[UIToolbar alloc] init];
	toolbar.barStyle = UIBarStyleBlackTranslucent;
	
	//Set the toolbar to fit the width of the app.
	[toolbar sizeToFit];
	
	//Caclulate the height of the toolbar
	CGFloat toolbarHeight = [toolbar frame].size.height;
	
	//Get the bounds of the parent view
	CGRect rootViewBounds = self.view.bounds;
	
	//Get the height of the parent view.
	CGFloat rootViewHeight = CGRectGetHeight(rootViewBounds);
	
	//Get the width of the parent view,
	CGFloat rootViewWidth = CGRectGetWidth(rootViewBounds);
	
	//Create a rectangle for the toolbar
	CGRect rectArea = CGRectMake(0, rootViewHeight - (toolbarHeight-20), rootViewWidth, toolbarHeight);
	
	//Reposition and resize the receiver
	[toolbar setFrame:rectArea];
	
	//Create a button
	UIBarButtonItem *loginButton = [[UIBarButtonItem alloc]
								   initWithTitle:NSLocalizedString(@"LoginKey",@"") style:UIBarButtonItemStyleBordered target:self action:@selector(btnLogin_Clicked:)];
	UIBarButtonItem *saveButton = [[UIBarButtonItem alloc]
									initWithTitle:NSLocalizedString(@"LoginSaveKey",@"") style:UIBarButtonItemStyleBordered target:self action:@selector(btnSave_Clicked:)];
	
	[toolbar setItems:[NSArray arrayWithObjects:loginButton,saveButton,nil]];
	
	//Add the toolbar as a subview to the navigation controller.
	[self.view addSubview:toolbar];	
	
    [super viewDidLoad];
}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (IBAction)btnSave_Clicked:(id)sender {
	NSLog(@"Save Clicked");
	[appDelegate setEngine:txtUsername.text password:txtPassword.text];
	//Get the bundle path
	NSString *bPath = [[NSBundle mainBundle] bundlePath];
	NSString *settingsPath = [bPath stringByAppendingPathComponent:@"Settings.bundle"];
	NSString *plistFile = [settingsPath stringByAppendingPathComponent:@"Root.plist"];
	
	//Get the Preferences Array from the dictionary
	NSDictionary *settingsDictionary = [NSDictionary dictionaryWithContentsOfFile:plistFile];
	NSArray *preferencesArray = [settingsDictionary objectForKey:@"PreferenceSpecifiers"];

	NSDictionary *item;
	NSString *name_preference;
	NSString *password_preference;
	NSString *items_preference;

	for(item in preferencesArray)
	{
		//Get the key of the item.
		NSString *keyValue = [item objectForKey:@"Key"];
		
		//Get the default value specified in the plist file.
		id defaultValue = [item objectForKey:@"DefaultValue"];
		
		if([keyValue isEqualToString:@"name_preference"])
			name_preference = txtUsername.text;
		
		if([keyValue isEqualToString:@"password_preference"])
			password_preference = txtPassword.text;
		
		if([keyValue isEqualToString:@"items_preference"])
			items_preference = defaultValue;
	}
	NSLog(@"Name %@, Pass: %@, Item: %d",name_preference,password_preference, items_preference);
	//Now that we have all the default values.
	//We will create it here.
	NSDictionary *appPrerfs = [NSDictionary dictionaryWithObjectsAndKeys:
							   name_preference, @"name_preference",
							   password_preference, @"password_preference",
							   items_preference, @"items_preference",
							   [NSNumber numberWithInt:20], @"items_preference",
							   nil];
	
	//Register and save the dictionary to the disk
	[[NSUserDefaults standardUserDefaults] registerDefaults:appPrerfs];
	[[NSUserDefaults standardUserDefaults] synchronize];	
	[appDelegate goHome];
	[self release];
}
- (IBAction)btnLogin_Clicked:(id)sender {
	NSLog(@"Login Clicked");
	[appDelegate setEngine:txtUsername.text password:txtPassword.text];
	[appDelegate goHome];
	[self release];
}
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
	[theTextField resignFirstResponder];
	return YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}


- (void)dealloc {
    [super dealloc];
}


@end
